﻿namespace CRM.DataAccess.DAOs
{
    using System.Collections.Generic;
    using System.Data.Entity;
    using CRM.DataAccess.Entities;
    using System.Linq;
    using System.Data.Entity.Validation;
    using System.Diagnostics;
    using System;

    public class DescriptionDetailDAO : DbContext
    {
        public DescriptionDetailDAO()
            : base("name=Entities")
        {
        }
        public DbSet<DESCRIPTION_DETAILS> DESCRIPTION_DETAILS { get; set; }
        public DbSet<USER> USERS { get; set; }

        //Get DESCRIPTION_DETAILS
        public List<DESCRIPTION_DETAILS> GetAllDescriptionDetails()
        {
            try
            {
                var desQuery = from d in DESCRIPTION_DETAILS
                               select d;
                List<DESCRIPTION_DETAILS> dds = desQuery.ToList();
                if (dds.Count > 0)
                    return dds;
                return null;
            }
            catch
            {
                return null;
            }
        }
        //Check exist DESCRIPTION_DETAILS
        public int CheckExist(string DesName)
        {
            try
            {
                var Query = from d in DESCRIPTION_DETAILS
                            where d.DESCRIPTION_NAME.ToUpper() == DesName.ToUpper()
                            select d;
                List<DESCRIPTION_DETAILS> dds = Query.ToList();
                if (dds.Count > 0)
                    return 1;
                return 0;
            }
            catch
            {
                return -1;
            }
        }

        //Add new DESCRIPTION_DETAILS
        public int AddNewDescription(DESCRIPTION_DETAILS dd)
        {
            try
            {
                this.DESCRIPTION_DETAILS.Add(dd);
                this.SaveChanges();
                return 1;
            }
            catch
            {
                return -1;
            }
        }

        //Get DESCRIPTION_DETAILS by Condition
        public List<DESCRIPTION_DETAILS> GetDescriptionByCondition(DateTime? dateFrom, DateTime? dateTo, string DesName)
        {
            try
            {
                var Query = from d in DESCRIPTION_DETAILS
                            where ((DesName != "" ? (d.DESCRIPTION_NAME.ToUpper().Contains(DesName.ToUpper())) : (true))
                                   && (d.DATE_CREATED >= dateFrom && d.DATE_CREATED < dateTo))
                            select d;
                List<DESCRIPTION_DETAILS> dds = Query.AsNoTracking().ToList();
                if (dds.Count > 0)
                    return dds;
                return null;
            }
            catch
            {
                return null;
            }
        }

        //Delete DESCRIPTION_DETAILS
        public int DeleteDescription(long ID)
        {
            try
            {
                DESCRIPTION_DETAILS.RemoveRange(DESCRIPTION_DETAILS.Where(x => x.ID == ID));
                this.SaveChanges();
                return 1;
            }
            catch
            {
                return -1;
            }
        }

        //Get DESCRIPTION_DETAILS by ID
        public DESCRIPTION_DETAILS GetDescriptionByID(long ID)
        {
            try
            {
                var Query = from d in DESCRIPTION_DETAILS
                            where d.ID == ID
                            select d;
                List<DESCRIPTION_DETAILS> dd = Query.ToList();
                if (dd.Count > 0)
                    return dd[0];
                return null;
            }
            catch
            {
                return null;
            }
        }
        //update DESCRIPTION_DETAILS
        public int UpdateDescription(DESCRIPTION_DETAILS dd)
        {
            try
            {
                var original = DESCRIPTION_DETAILS.Find(dd.ID);
                if (original != null)
                {
                    Entry(original).CurrentValues.SetValues(dd);
                    SaveChanges();
                    return 1;
                }
                return -1;
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}",
                                                validationError.PropertyName,
                                                validationError.ErrorMessage);
                    }
                }
                return -1;
            }
        }
    }
}
